Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(wallet): only mark change address used if create_tx succeeds #1579

Merged

Conversation

ValuedMammal
Copy link
Contributor

@ValuedMammal ValuedMammal commented Aug 27, 2024

If no drain script is specified in tx params then we get it from the change keychain by looking at the next unused address. Before this PR we marked the index used regardless of whether a change output is finally added. Then if creating a psbt failed, we never restored the unused status of the change address, so creating the next tx would have revealed an extra one.

We want to mark the change address used so that other callers won't attempt to use the same address between the time we create the tx and when it appears on chain. With this PR we only mark the change address used if we successfully create a psbt and the drain script is used in the change output.

fixes #1578

Notes to the reviewers

An early idea was to unmark the change address used if we fail to create a tx due to InsufficientFunds, but after looking into it I figure it doesn't totally make sense to mark the address used before we've determined that a change output is necessary. Further, create_tx can fail in other ways besides running coin selection, so I moved the mark_used logic to the end of the function.

Changelog notice

Fixed an issue that caused an unused internal address to be skipped when creating transactions (#1578)

Checklists

All Submissions:

  • I've signed all my commits
  • I followed the contribution guidelines
  • I ran cargo fmt and cargo clippy before committing

Bugfixes:

  • I've added tests to reproduce the issue which are now passing
  • I'm linking the issue being fixed by this PR

@ValuedMammal ValuedMammal self-assigned this Aug 27, 2024
@ValuedMammal ValuedMammal marked this pull request as ready for review August 27, 2024 22:07
@ValuedMammal ValuedMammal added this to the 1.0.0-beta milestone Aug 27, 2024
@ValuedMammal
Copy link
Contributor Author

This might also be related to #1577

@notmandatory notmandatory added the bug Something isn't working label Aug 28, 2024
Copy link
Member

@evanlinjin evanlinjin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for taking this forward. Let me know what you think about the next_unused_spk_without_revealing idea.

crates/wallet/src/wallet/mod.rs Outdated Show resolved Hide resolved
crates/wallet/src/wallet/mod.rs Outdated Show resolved Hide resolved
Copy link
Member

@evanlinjin evanlinjin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ACK 18ecd06

crates/wallet/src/wallet/mod.rs Outdated Show resolved Hide resolved
crates/wallet/tests/wallet.rs Outdated Show resolved Hide resolved
Copy link
Contributor

@oleonardolima oleonardolima left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

utACK 004b0ca

It looks good! Thanks for addressing this issue.
I left some suggestions and nit below.

@ValuedMammal ValuedMammal force-pushed the fix/wallet-change-index-unused branch 2 times, most recently from 51fe144 to 849e136 Compare September 7, 2024 17:05
Copy link
Contributor

@oleonardolima oleonardolima left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ACK 849e136

If no drain script is specified in tx params then we get it from
the change keychain by looking at the next unused address. We want
to mark the change address used so that other callers won't attempt
to use the same address between the time we create the tx and when
it appears on chain.

Before, we marked the index used regardless of whether a change
output is finally added. Then if creating a PSBT failed, we never
restored the unused status of the change address, so creating the
next tx would have revealed an extra one. Now we only mark the change
address used if we successfully create a PSBT and the drain script
is used in the change output.
@ValuedMammal
Copy link
Contributor Author

Made last minute changes @notmandatory

Copy link
Member

@notmandatory notmandatory left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ACK 606fa08

@notmandatory notmandatory merged commit b49b64b into bitcoindevkit:master Sep 9, 2024
21 checks passed
@ValuedMammal ValuedMammal deleted the fix/wallet-change-index-unused branch September 10, 2024 04:26
This was referenced Sep 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

PSBT Creation: Change Address Index Skipped on InsufficientFunds
4 participants